package maincode;
import java.io.*; 
import java.net.*; 
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class dataOfDate {
//	 public static void main(String args[]) { 
//		  System.out.print(getDataAtDate(16329,01)); 
//	    } 
	
	
	public static double getDataAtDate(long time, int tar){
		double exRate = 0;
		String curr[] = {"usd","cny","jpy","eur","aud","gbp"};
		
		String link = "http://currencies.apps.grandtrunk.net/getrate/";
		
		Date date = new Date(time*(24*1000*3600)+5);
		DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd");
		String t = formatter.format(date);
		
		link = link + t + "/";
		
		
		String cur = curr[tar];
		
		link = link + cur + "/hkd";
		
		//System.out.print(link);
		
		InputStream in = null;  
		
		try {		
			URL url = new URL(link); 
			in = url.openStream();
			
			byte[] buffer = new byte[4096]; 
			int bytes_read;  
			
			while((bytes_read = in.read(buffer)) != -1)
				continue; 
			String num = new String(buffer, "UTF-8");
			exRate = Double.parseDouble(num);
		}  
		catch (Exception e) {
//			System.err.println(e);  
//			System.err.println("Usage: java GetURL <URL> [<filename>]");
			return -1;
		}  
		finally{ //close the stream in any case 
			try {
				in.close();
			} 
			catch (Exception e) {} 
		}
		return exRate;
	}
}
